import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.rmi.*;
//import java.rmi.Naming;
//import java.rmi.NotBoundException;
//import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author ulan
 */

public class RMIDegClient extends javax.swing.JFrame {

    String input;
    double result;
    static RMIDegIntf fungsi;
            
    public RMIDegClient() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        inputTxt = new javax.swing.JTextField();
        convertBtn = new javax.swing.JButton();
        clearBtn = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        celcius1 = new javax.swing.JRadioButton();
        fahrenheit1 = new javax.swing.JRadioButton();
        kelvin1 = new javax.swing.JRadioButton();
        celcius2 = new javax.swing.JRadioButton();
        fahrenheit2 = new javax.swing.JRadioButton();
        kelvin2 = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Program Konversi Suhu");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tw Cen MT", 1, 18)); // NOI18N
        jLabel1.setText("PROGRAM KONVERSI SUHU");

        inputTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                inputTxtKeyTyped(evt);
            }
        });

        convertBtn.setText("KONVERSI");
        convertBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertBtnActionPerformed(evt);
            }
        });

        clearBtn.setText("HAPUS");
        clearBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearBtnActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("FROM");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("TO");

        buttonGroup1.add(celcius1);
        celcius1.setText("CELCIUS");

        buttonGroup1.add(fahrenheit1);
        fahrenheit1.setText("FAHRENHEIT");

        buttonGroup1.add(kelvin1);
        kelvin1.setText("KELVIN");

        buttonGroup2.add(celcius2);
        celcius2.setText("CELCIUS");

        buttonGroup2.add(fahrenheit2);
        fahrenheit2.setText("FAHRENHEIT");

        buttonGroup2.add(kelvin2);
        kelvin2.setText("KELVIN");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Suhu :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(41, 41, 41)
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(celcius1)
                                    .addComponent(convertBtn)
                                    .addComponent(fahrenheit1)
                                    .addComponent(kelvin1))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(celcius2)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(95, 95, 95))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(clearBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(64, 64, 64))))
                            .addComponent(fahrenheit2)
                            .addComponent(kelvin2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(inputTxt))
                        .addContainerGap(86, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(convertBtn)
                    .addComponent(clearBtn))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(celcius1)
                    .addComponent(celcius2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fahrenheit1)
                    .addComponent(fahrenheit2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kelvin1)
                    .addComponent(kelvin2))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearBtnActionPerformed
        inputTxt.setText("");
    }//GEN-LAST:event_clearBtnActionPerformed

    private void inputTxtKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputTxtKeyTyped
        
        char c =  evt.getKeyChar();
        
        if(!Character.isDigit(c)){
            
            if(c == '.' || c == '\b') {}
            else {
                Toolkit.getDefaultToolkit().beep();
            }
        }
        
        
    }//GEN-LAST:event_inputTxtKeyTyped

    private void convertBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertBtnActionPerformed
       
        try {
            // klo FROM dan TO sama
            if (celcius1.isSelected() && celcius2.isSelected()){
                input = inputTxt.getText();
                
                if(input.equals("")){
                    JOptionPane.showMessageDialog(null, "Masukkan suhu yang akan di konversi!");
                
                } else {
                    JOptionPane.showMessageDialog(null, "FROM dan TO tidak boleh sama!");
                }
                
            } else if (fahrenheit1.isSelected() && fahrenheit2.isSelected()){
                input = inputTxt.getText();
                
                if(input.equals("")){
                    JOptionPane.showMessageDialog(null, "Masukkan suhu yang akan di konversi!");
                
                } else {
                    JOptionPane.showMessageDialog(null, "FROM dan TO tidak boleh sama!");
                }
                
            } else if (kelvin1.isSelected() && kelvin2.isSelected()){
                input = inputTxt.getText();
                
                if(input.equals("")){
                    JOptionPane.showMessageDialog(null, "Masukkan suhu yang akan di konversi!");
                
                } else {
                    JOptionPane.showMessageDialog(null, "FROM dan TO tidak boleh sama!");
                }
              
            // celcius    
            } else if (celcius1.isSelected() && fahrenheit2.isSelected()){
                input = inputTxt.getText();
                if(input.equals("")){
                    JOptionPane.showMessageDialog(null, "Masukkan suhu yang akan di konversi!");
                
                } else {
                    result = fungsi.celToFah(Double.parseDouble(input));
                    JOptionPane.showMessageDialog(null, "Hasil konversi "+input+" Celcius ke Fahrenheit adalah "+ result +" Fahrenheit");
                }
                
            } else if (celcius1.isSelected() && kelvin2.isSelected()){
                input = inputTxt.getText();
                if(input.equals("")){
                    JOptionPane.showMessageDialog(null, "Masukkan suhu yang akan di konversi!");
                
                } else {
                    result = fungsi.celToKel(Double.parseDouble(input));
                    JOptionPane.showMessageDialog(null, "Hasil konversi "+input+" Celcius ke Kelvin adalah "+ result +" Kelvin");
                }
            
            // fahrenheit    
            } else if (fahrenheit1.isSelected() && celcius2.isSelected()){
                input = inputTxt.getText();
                if(input.equals("")){
                    JOptionPane.showMessageDialog(null, "Masukkan suhu yang akan di konversi!");
                
                } else {
                    result = fungsi.fahToCel(Double.parseDouble(input));
                    JOptionPane.showMessageDialog(null, "Hasil konversi "+input+" Fahrenheit ke Celcius adalah "+ result +" Celcius");
                }
                
            } else if (fahrenheit1.isSelected() && kelvin2.isSelected()){
                input = inputTxt.getText();
                if(input.equals("")){
                    JOptionPane.showMessageDialog(null, "Masukkan suhu yang akan di konversi!");
                
                } else {
                    result = fungsi.fahToKel(Double.parseDouble(input));
                    JOptionPane.showMessageDialog(null, "Hasil konversi "+input+" Fahrenheit ke Kelvin adalah "+ result +" Kelvin");
                }
             
            // Kelvin    
            } else if (kelvin1.isSelected() && celcius2.isSelected()){
                input = inputTxt.getText();
                if(input.equals("")){
                    JOptionPane.showMessageDialog(null, "Masukkan suhu yang akan di konversi!");
                
                } else {
                    result = fungsi.kelToCel(Double.parseDouble(input));
                    JOptionPane.showMessageDialog(null, "Hasil konversi "+ input +" Kelvin ke Celcius adalah "+ result +" Celcius");
                }
                
            } else if (kelvin1.isSelected() && fahrenheit2.isSelected()){
                input = inputTxt.getText();
                if(input.equals("")){
                    JOptionPane.showMessageDialog(null, "Masukkan suhu yang akan di konversi!");
                
                } else {
                    result = fungsi.kelToFah(Double.parseDouble(input));
                    JOptionPane.showMessageDialog(null, "Hasil konversi "+input+" Kelvin ke Fahrenheit adalah "+result+" Fahrenheit");
                }
                
            }    
            
        } catch (Exception e) {
        }
        
    }//GEN-LAST:event_convertBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            String regis = "localhost";
            
            if(args.length >= 1){
                regis = args[0];
            }
            
            String reg = "rmi://"+regis+"/Degree";
            fungsi = (RMIDegIntf) Naming.lookup(reg);
            
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RMIDegClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RMIDegClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RMIDegClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RMIDegClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(RMIDegClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(RMIDegClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(RMIDegClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RMIDegClient().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JRadioButton celcius1;
    private javax.swing.JRadioButton celcius2;
    private javax.swing.JButton clearBtn;
    private javax.swing.JButton convertBtn;
    private javax.swing.JRadioButton fahrenheit1;
    private javax.swing.JRadioButton fahrenheit2;
    private javax.swing.JTextField inputTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton kelvin1;
    private javax.swing.JRadioButton kelvin2;
    // End of variables declaration//GEN-END:variables
}
